package com.banciyuan.bcywebview.biz.main.group.hot.viewholder;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Team;

/**
 * Created by squall on 2017/4/21.
 */

public class ViewGroupPic extends ViewGroupBase {

    public ImageView bestIv;

    public ViewGroupPic(View convertView) {
        super(convertView);
        bestIv = (ImageView) convertView.findViewById(R.id.best_img);
    }

    public void renderData(final Context mContext, ViewGroupPic viewGroupPic, final Team team, int type) {
        super.renderGroupTagData(mContext, viewGroupPic.tag_text, viewGroupPic.workForm, team);
        setPostNum(viewGroupPic, team, type);
        if (!TextUtils.isEmpty(team.getName())) {
            viewGroupPic.title.setText(Html.fromHtml(team.getName()));
        }
        if (type != 2) {
            if (team.getPosts() != null && !team.getPosts().isEmpty()) {
                viewGroupPic.bestPost.setVisibility(View.VISIBLE);
                viewGroupPic.bestPost.setText(Html.fromHtml(team.getPosts().get(0).getPlain()));
                if (!TextUtils.isEmpty(team.getPosts().get(0).getImg_src())) {
                    viewGroupPic.bestIv.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(team.getPosts().get(0).getImg_src(), viewGroupPic.bestIv, ImageOptions.options);
                } else {
                    viewGroupPic.bestIv.setVisibility(View.GONE);
                }
            } else {
                viewGroupPic.bestPost.setVisibility(View.GONE);
                viewGroupPic.bestIv.setVisibility(View.GONE);
            }
        } else {
            viewGroupPic.bestPost.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(team.getImg_src())) {
                viewGroupPic.bestIv.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(team.getImg_src(), viewGroupPic.bestIv, ImageOptions.options);
            } else {
                viewGroupPic.bestIv.setVisibility(View.GONE);
            }
        }
        ((View) (viewGroupPic.bestIv.getParent())).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, team.getGid(), team.getName());
            }
        });
    }

    private void setPostNum(ViewGroupPic viewGroupPic, Team team, int type) {
        if (!TextUtils.isEmpty(team.getPost_num())) {
            int postNum = Integer.parseInt(team.getPost_num());
            if (type == -1) {
                if (postNum > 10) {
                    viewGroupPic.post_num.setText(String.format(talkGroupFormat, team.getPost_num()));
                } else {
                    viewGroupPic.post_num.setText(joinFormat);
                }
            } else if (type == 2) {
                if (postNum > 0) {
                    viewGroupPic.post_num.setText(String.format(joinGroupFormat, team.getPost_num()));
                } else {
                    viewGroupPic.post_num.setText(joinFormat);
                }
            } else {
                viewGroupPic.post_num.setText(String.format(talkGroupFormat, team.getPost_num()));
            }
        } else {
            viewGroupPic.post_num.setText(joinFormat);
        }
    }
}
